import unittest, requests
# 导入数据库类
from Mysql import mysql


class Weidu(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')
        # 初始化数据库类
        # self.db = mysql('10.59.9.19', 'yidongceshi', 'Yd123456!', 'mall')

    def tearDown(self) -> None:
        print('end case')
        # self.db.close()

    # 登录
    userId = None
    sessionId = None
    phone = None

    def test_Weidu_01(self):
        r1 = requests.post(url='http://10.59.9.18/small/user/v1/login',
                           json={"phone": "15535738398", "pwd": "zjj031613"})
        print(r1.json())
        # 提取userId sessionId
        Weidu.userId = r1.json()['result']['userId']
        Weidu.sessionId = r1.json()['result']['sessionId']
        # # 断言 提取phone
        # Weidu.phone = r1.json()['result']['phone']
        # print(Weidu.phone)
        # result = self.db.chaxun(f'select * from user where phone={str(Weidu.phone)}')
        # print(result)
        # # 提取用户名 进行对比和数据库
        # nickName = r1.json()['result']['nickName']
        # print(nickName)
        # self.assertEqual(nickName, result[0][1])

    # 修改昵称
    def test_Weidu_02(self):
        r2 = requests.put(url='http://10.59.9.18/small/user/verify/v1/modifyUserNick',
                          headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                          data={"nickName": "天天快乐"})
        print(r2.json())
        # # 断言 修改昵称后 查看数据库是否一致
        # result = self.db.chaxun(f'select * from user where phone={str(Weidu.phone)}')
        # print(result)
        # self.assertTrue("天天快乐" == result[0][1])

    # 添加新收货地址
    addres_id = None

    def test_Weidu_03(self):
        r3 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"realName": "小婷", "phone": "18185603008",
                                 "address": "北京昌平区南口镇21号", "zipCode": 101077})
        print(r3.json())
        # 断言 查看数据库是否存在新添加的收获地址
        # zipCode = 101077
        # result = self.db.chaxun(f'select * from receive_address where zip_Code={zipCode}')
        # print(result)
        # self.assertIn("北京昌平区南口镇21号", result[0][4])
        # message = r3.json()['message']
        # self.assertIn('添加成功', message)
        # # 提出地址id
        # Weidu.addres_id = result[0][0]

    # 设置默认收货地址
    message4 = None

    def test_Weidu_04(self):
        r4 = requests.post(url='http://10.59.9.18/small/user/verify/v1/setDefaultReceiveAddress',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           data={"id": Weidu.addres_id})
        print(r4.json())
        # # 数据库断言根据 收获地址id 查询改地址用户名 是否和添加的一致
        # result = self.db.chaxun(f'select * from receive_address where id={Weidu.addres_id}')
        # print(result)
        # self.assertTrue('小婷' == result[0][2])
        # 响应断言
        # Weidu.message4 = r4.json()['message']
        # self.assertEqual('设置成功', Weidu.message4)

    # 查询购物车
    commodityId = None
    price = None
    def test_Weidu_05(self):
        r5 = requests.get(url='http://10.59.9.18/small/order/verify/v1/findShoppingCart',
                          headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)})
        print(r5.json())
        categoryName = r5.json()['result'][0]['shoppingCartList'][0]['commodityName']
        print(categoryName)
        # # 数据库断言 提取商品ID 通过ID 查看是数据库中是否存该商品
        # Weidu.commodityId = r5.json()['result'][0]['shoppingCartList'][0]['commodityId']
        # result = self.db.chaxun(f'select * from commodity where id={Weidu.commodityId}')
        # print(result)
        # self.assertEqual(categoryName, result[0][1])
        # # 响应断言
        # self.assertTrue(Weidu.commodityId == result[0][0])
        # Weidu.price = r5.json()['result'][0]['shoppingCartList'][0]['price']

    # 创建订单
    order_id=None
    def test_Weidu_06(self):
        r6 = requests.post(url='http://10.59.9.18/small/order/verify/v1/createOrder',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           json={"commodityId": Weidu.commodityId, "amount": 1, "totalPrice": Weidu.price,
                                 "addressId": Weidu.addres_id})
        print(r6.json())
        # 响应断言
        timestamp=r6.json()['timestamp']
        Weidu.order_id='2022062411320921627752'
        print(timestamp)
        self.assertNotEqual('2025-04-26',timestamp)

    # 支付
    message7=None
    status=None
    def test_Weidu_07(self):
        r7 = requests.post(url='http://10.59.9.18/small/order/verify/v1/pay',
                           headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                           params={"orderId": Weidu.order_id, "amount": 1, "payType": 1})
        print(r7.json())
        # 响应断言
        Weidu.message7=r7.json()['message']
        Weidu.status = r7.json()['status']
        self.assertFalse('0000'==Weidu.status)
        self.assertTrue('该订单已支付'==Weidu.message7)

    # 查看订单状态
    def test_Weidu_08(self):
        r8 = requests.get(url='http://10.59.9.18/small/order/verify/v1/findOrderListByStatus',
                          headers={'userId': str(Weidu.userId), 'sessionId': str(Weidu.sessionId)},
                          params={"status":1,"page":1,"count":5})
        print(r8.json())
        # 数据库断言 根据商品id 查看数据库中订单号是否生成
        # result = self.db.chaxun(f'select * from order_detail where commodity_Id={Weidu.commodityId}')
        # print(result)
        # self.assertTrue(Weidu.order_id == result[0][1])



if __name__ == '__main__':
    # 第一种方式
    unittest.main()
    # 第二种方式
    # suite=unittest.TestSuite()
    # unittest.defaultTestLoader.discover(start_dir='./',pattern='Weidu*.py')
    # runner = unittest.TextTestRunner()
    # runner.run(suite)
    # 第三种方式
    # suite = unittest.TestSuite()
    # suite.addTest(Weidu.test_Weidu_01())
    # suite.addTest(Weidu.test_Weidu_02())
    # suite.addTest(Weidu.test_Weidu_03())
    # suite.addTest(Weidu.test_Weidu_04())
    # suite.addTest(Weidu.test_Weidu_05())
    # suite.addTest(Weidu.test_Weidu_06())
    # runner = unittest.TextTestRunner()
    # runner.run(suite)
